// pages/login/login.js
const api=require('../../utils/request') 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    type: '',
    reportId:'',
    isAfterShow: false,
    image:'',
    backType:'',
    phone:'',
    password:''
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
        this.setData({
            type: options.type,
            reportId: options.reportId,
            backType: options.backType
        })
      
   

},
  checkedTap(){ 
      if(this.data.checked){
          this.setData({
              checked: false
          })
      }else{
        this.setData({
            checked: true
        }) 
      }
       
  },
  checkgetPhoneNumber(){
    wx.showToast({
        title: '请阅读并同意相关协议',
        icon: 'none',
        duration: 2000,
      })
   


  },
password(){
    wx.navigateTo({
        url: '../psd/psd'
      })
},

  loginAfterClose(){
      this.setData({
        isAfterShow: false
      })
      this.backPage()
  },
 
  login(){
      let that=this
      if(this.data.phone===''){
        wx.showToast({
            title: '请输入手机号码',
            icon: 'none',
            duration: 2000,
          })
          return false
      }
      if(this.data.password===''){
        wx.showToast({
            title: '请输入密码',
            icon: 'none',
            duration: 2000,
          })
          return false
      }
      if(!this.data.checked){
        wx.showToast({
            title: '请阅读并同意相关协议',
            icon: 'none',
            duration: 2000,
          })
          return false
      }
    wx.login({
      success: (res) => {
        console.log(res.code)
        api.post('wechat/loginPhone',{code:res.code,phone:that.data.phone,password:that.data.password}).then(response=>{
            if(response.code===200){
                debugger
                let user=response.data;
                wx.setStorageSync('user',JSON.stringify(user));  
                if(user.isShow){
                    that.setData({
                        isAfterShow: true,
                        image: user.image
                       })
                }else{
                     that.backPage()
                    }
            }else{
                wx.showToast({
                    title: response.msg,
                    icon: 'none',
                    duration: 2000,
                  })
            }
        })
      },
    })


  },
  value(e){
      this.setData({
          phone: e.detail.value
      })
  },
  value2(e){
    this.setData({
        password: e.detail.value
    })
},
backPage(){
    if(this.data.type==='1'){
        wx.switchTab({
            url: '/pages/report/report',
            success: function (e) {
              let page = getCurrentPages().pop(); 
              page.onLoad();
            }
          })
    }else if(this.data.type==='2'){
        wx.redirectTo({
            url: '../vip/buy_vip/buy_vip'
          })
          
         
    }else if(this.data.type==='3'){
         
        wx.redirectTo({
            url: '/pages/report/pdf/pdf?id='+this.data.reportId+'&backType='+this.data.backType
          })
    }else{
        wx.switchTab({
            url: '/pages/mine/mine',
            success: function (e) {
              let page = getCurrentPages().pop(); 
              page.onLoad();
            }
          })
    }
},
 
//用户协议
user_xy(){
  wx.navigateTo({
    url: '/pages/mine/contact_us/user_agreement/user_agreement',
  })
},
//用户隐私
user_ys(){
  wx.navigateTo({
    url: '/pages/mine/contact_us/privacy_policy/privacy_policy',
  })
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
      console.log('eeee')
     

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})